"""
Case Type   : HTAP行列融合特性
Case Name   : 设置max_imcs_cache为100MB,大量数据耗尽缓冲区生成临时文件
Create At   : 2025/1/7
Owner       : ningyali
Description :
    1.设置enable_memory_limit为on
    2.设置max_imcs_cache为100M
    3.重启数据库
    4.查看GS_TOTAL_MEMORY_DETAIL函数
    5.建表插入5000000行数据
    6.进行行列转换
    7.查看GS_TOTAL_MEMORY_DETAIL函数
    8.查看query_imcstore_views()
    9.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功，max_imcs_cache设置值生效
    5.执行成功
    6.执行成功
    7.缓冲区未耗尽，未创建临时文件
    8.缓冲区未耗尽，未创建临时文件
    9.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_single_0130'
        self.tmp_path = os.path.join(
            macro.SINGLE_DB_INSTANCE_PATH, "base", "imcu")

    def test_trans(self):
        text = '----step1:设置enable_memory_limit为on;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_memory_limit=on')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        text = '----step2:设置max_imcs_cache为100M; expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache=100MB')
        self.assertEqual(True, guc_res, f'参数配置失败:{text}')

        text = '----step3:重启数据库; expect:执行成功----'
        self.log.info(text)
        is_restarted = self.pri_sh.restart_db_cluster()
        self.assertTrue(is_restarted, f'执行失败: {text}')

        text = '----step4:查看GS_TOTAL_MEMORY_DETAIL函数; expect:执行成功----'
        self.log.info(text)
        sql = f'select * from gs_total_memory_detail;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect = r'imcstore_max_memory\s+|\s+100'
        self.assertRegex(sql_res, expect, f'执行失败: {text}')

        text = '----step5:建表插入5000000行数据; expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};' \
              f'create table {self.t_name}(' \
              f'    c1 integer,' \
              f'    c2 varchar(30),' \
              f'    c3 char(30));' \
              f'insert into {self.t_name} values (' \
              f'generate_series(1, 5000000),' \
              f'generate_series(1, 5000000)||\'aaa\',' \
              f'generate_series(1, 5000000)||\'bbb\');' \
              f'\\d+'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect_res = r'CREATE TABLE\s+INSERT 0 5000000'
        self.assertRegex(sql_res, expect_res, f'建表插入数据失败:{text}')

        text = '----step6:进行行列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'alter table {self.t_name} imcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('ALTER TABLE', sql_res, f'行列转换失败:{text}')

        text = '----step7:查看GS_TOTAL_MEMORY_DETAIL函数; expect:缓冲区耗尽，创建临时文件----'
        self.log.info(text)
        sql = f"select memorymbytes from gs_total_memory_detail " \
              f"where memorytype='imcstore_used_memory';"
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows, _ = self.common.parse_sql_query_res(sql_res)
        # 如果存在内存碎片，将无法恰好占满100MB，因此断言使用量90~100
        self.assertGreater(int(rows[0][0]), 90, f'缓冲区未使用:{text}')
        self.assertLess(int(rows[0][0]), 100, f'缓冲区耗尽:{text}')

        text = '----step8:查看query_imcstore_views(); expect:缓冲区耗尽，创建临时文件----'
        self.log.info(text)
        sql = f'select cu_size_in_mem,cu_num_in_mem,' \
              f'cu_size_in_disk,cu_num_in_disk ' \
              f'from query_imcstore_views() ' \
              f'where relname like \'{self.t_name}%\';'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows, _ = self.common.parse_sql_query_res(sql_res)
        self.assertGreater(int(rows[0][-1]), 0, f'未生成临时文件:{text}')
        self.assertGreater(int(rows[0][-2]), 0, f'未成临时文件:{text}')

        ls_cmd = f'ls {self.tmp_path}'
        ls_res = self.common.get_sh_result(self.pri_sh.node, ls_cmd)
        self.assertGreater(len(ls_res.strip().splitlines()), 0,
                           f'未成临时文件:{text}')

    def tearDown(self):
        text = '----step9:清理环境;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)

        guc_res1 = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_memory_limit')

        guc_res2 = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache')

        is_restarted = self.pri_sh.restart_db_cluster()

        self.assertIn('DROP TABLE', sql_res, f'删除表失败:{text}')
        self.assertEqual(True, guc_res1, f'参数配置失败:{text}')
        self.assertEqual(True, guc_res2, f'参数配置失败:{text}')
        self.assertTrue(is_restarted, f'执行失败: {text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
